package OrederManagement.command;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import OrederManagement.command.impl.BuyCommand;
import OrederManagement.command.impl.CustomerDeleteCommand;
import OrederManagement.command.impl.CustomerModifyCommand;
import OrederManagement.command.impl.CustomerSelectCommand;
import OrederManagement.command.impl.ExitCommand;
import OrederManagement.command.impl.HelpCustomerCommand;
import OrederManagement.pojo.User;

public class UserCommandFactory extends SystemCommandFactory {

	private static Map<CommandCode,Class<? extends Command>> userCommandMap =new HashMap<>();
	
	static{
		userCommandMap.put(CommandCode.HELP, HelpCustomerCommand.class);
		userCommandMap.put(CommandCode.BUY, BuyCommand.class);
		userCommandMap.put(CommandCode.SELECT, CustomerSelectCommand.class);
		userCommandMap.put(CommandCode.DELETE, CustomerDeleteCommand.class);
		userCommandMap.put(CommandCode.EXIT, ExitCommand.class);		
	}
	private User user;
	
	public UserCommandFactory(User user){
		this.user=user;
	}
	public User getUser(){
		return user;
	}
	@Override
	public Command buildCommand(CommandCode cmdCode) {
		Class<?extends Command> cmdClass =userCommandMap.get(cmdCode);
		if(cmdClass==null){
			return super.buildCommand(cmdCode);
		}
		Command command =null;
		try {
			command=cmdClass.getConstructor(User.class).newInstance(user);
		} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
				| NoSuchMethodException | SecurityException e) {
			e.printStackTrace();
		}
		
		return command;
	}

}
